package com.woniuxy.courseweb.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.coursedao.model.TeamVacate;
import com.woniuxy.coursemodel.dto.TeamVacateDto;
import com.woniuxy.coursemodel.form.UserVacatePageForm;
import com.woniuxy.coursemodel.form.VenueVacatePageForm;
import com.woniuxy.coursemodel.param.VacatePageParam;
import com.woniuxy.courseservice.service.TeamCourseService;
import com.woniuxy.courseservice.service.TeamVacateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 团课请假记录表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/teamVacate")
@Api(tags = "团课请假相关接口")
public class TeamVacateController {

    @Resource
    private TeamVacateService teamVacateService;

    /**
     *
     * @param teamCourseIds 团课课程id集合
     * @param reason 请假理由
     * @param token 用户token
     * @return 无
     */
    @ApiOperation("团课请假方法")
    @GetMapping("/vacate")
    public Result<?> vacate(List<Integer> teamCourseIds,String reason ,@RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        teamVacateService.vacate(teamCourseIds,reason,userId);
        return Result.success("团课请假成功");
    }

    /**
     * @param form (页码、条数）
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("场馆分页查看请假方法")
    @PostMapping("/venueByPage")
    public Result<PageInfo<TeamVacateDto>> byPage(@RequestBody @Validated VenueVacatePageForm form, @RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        VacatePageParam param = BeanUtil.toBean(form, VacatePageParam.class);
        param.setVenueId(venueId);
        PageInfo<TeamVacateDto> pageInfo = teamVacateService.byPage(param);
        return Result.success("场馆分页查看请假成功",pageInfo);
    }

    /**
     * @param form (页码、条数）
     * @param token 用户token
     * @return 无
     */
    @ApiOperation("用户分页查看请假方法")
    @PostMapping("/userByPage")
    public Result<PageInfo<TeamVacateDto>> byPage(@RequestBody @Validated UserVacatePageForm form, @RequestHeader String token){
        Integer userId = MyJwtUtil.getUserIdByKey(token, "userId");
        VacatePageParam param = BeanUtil.toBean(form, VacatePageParam.class);
        param.setUserId(userId);
        PageInfo<TeamVacateDto> pageInfo = teamVacateService.byPage(param);
        return Result.success("场馆分页查看请假成功",pageInfo);
    }

    @ApiOperation("通过id查看请假方法")
    @GetMapping("/byId")
    private Result<TeamVacateDto> byId(Integer teamVacateId){
        TeamVacateDto dto = teamVacateService.byId(teamVacateId);
        return Result.success("通过id查看请假成功",dto);
    }

    /**
     *
     * @param teamVacateId 请假id
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("同意请假方法")
    @GetMapping("/agree")
    public Result<?> agree(Integer teamVacateId ,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamVacateService.agree(teamVacateId,venueId);
        return Result.success("同意请假成功");
    }

    /**
     *
     * @param teamVacateId 请假id
     * @param token 场馆token
     * @return 无
     */
    @ApiOperation("不同意请假方法")
    @GetMapping("/disAgree")
    public Result<?> disAgree(Integer teamVacateId ,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        teamVacateService.disAgree(teamVacateId,venueId);
        return Result.success("不同意请假成功");
    }
}

